package com.delaphone.flocash.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.delaphone.flocash.web.bean.SelectList;
import com.delaphone.flocash.service.UserService;
import com.delaphone.flocash.util.StringUtil;
import com.opensymphony.xwork2.Action;
import org.jpos.gl.GLEntry;

/**
 *
 * @author dongtv
 */
public class HistoryAction extends BaseAction {

    private String search;
    private String date_from;
    private String date_to;
    private ArrayList<SelectList> searchList;
    private UserService userService;
    private List<GLEntry> trans;

    public String index() {
        Date now = new Date();
        Date fromD = StringUtil.addDate(now, -30);// 1 month ago
        Date toD = StringUtil.addDate(now, 1);
        date_from = StringUtil.formatDisplayDate(fromD);
        date_to = StringUtil.formatDisplayDate(toD);
        trans = userService.getTransHistory(user.getEmail(), user.getCurrency(), StringUtil.getDateForSQL(fromD), StringUtil.getDateForSQL(toD), "-");
        return Action.INPUT;
    }

    @Override
    public String execute() {
        if (user == null) {
            return Action.LOGIN;
        }
        trans = userService.getTransHistory(user.getEmail(), user.getCurrency(), date_from.replaceAll("-", ""), date_to.replaceAll("-", ""), search);
        request.setAttribute("sch", search);
        return Action.SUCCESS;
    }

// <editor-fold defaultstate="collapsed" desc="set and get method">
    public String getDate_from() {
        return date_from;
    }

    public void setDate_from(String date_from) {
        this.date_from = date_from;
    }

    public String getDate_to() {
        return date_to;
    }

    public void setDate_to(String date_to) {
        this.date_to = date_to;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public ArrayList<SelectList> getSearchList() {
        searchList = new ArrayList<SelectList>();
        searchList.add(new SelectList("", "All Transactions"));
        searchList.add(new SelectList("-", "Only Debits"));
        searchList.add(new SelectList("+", "Only Credits"));
        return searchList;
    }

    public void setSearchList(ArrayList<SelectList> searchList) {
        this.searchList = searchList;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public List<GLEntry> getTrans() {
        return trans;
    }

    public void setTrans(List<GLEntry> trans) {
        this.trans = trans;
    }
    // </editor-fold> 
}
